@extends('WShop::layout')

@section('content')
    @component('WShop::public.header')
        @slot('page_title'){{$page_title}} @endslot
    @endcomponent
    <div class="mui-content mui-scroll-wrapper" id="pullrefresh">
        <div class="mui-scroll">
            <div class="mui-table-view mui-table-view-chevron mui-bottom">
                <div class="mui-dizhi-guanli ">
                    @if($address->isNotEmpty())
                    <!-- 收货地址列表 -->
                    <ul class="mui-table-view">
                        @foreach($address as $item)
                        <li class="mui-table-view-cell  bg-fff">
                            <div>收货人：{{$item->consignee}} <span class="mui-pull-right">{{$item->phone}}</span></div>
                            <p class="mui-ellipsis-2">{{get_address($item->area)}} {{$item->address}}</p>
                            <div class="muidizhi-shezhi mui-clearfix">
                                <label for="dizhi" class="mui-pull-left mui-active">
                                    @if($item->is_default == 1)
                                        <button disabled class="mui-btn mui-btn-xs mui-disabled mui-btn-danger mui-btn-outlined">默认地址</button>
                                    @else
                                       <button class="mui-btn mui-btn-xs mui-btn-danger" onclick="optAddr(this)"
                                               data-loading-text="设置中"
                                               data-url="{{route('f_Wap_Address_setDefault',['addr_id'=>$item->id])}}"
                                       >设为默认</button>
                                    @endif
                                </label>
                                <div class="mui-pull-right">
                                    <button  class="mui-btn mui-btn-xs mui-btn-primary" onclick="window.location = '{{route('f_Wap_Address_update',['addr_id'=>$item->id])}}';" >编辑</button>
                                    <button onclick="optAddr(this)" class="mui-btn mui-btn-xs mui-btn-danger"  data-url="{{route('f_Wap_Address_delete',['addr_id'=>$item->id])}}">删除</button>
                                </div>
                            </div>
                        </li>
                        @endforeach
                    </ul>

                    @else
                    <div class="zanwu mui-text-center" style="margin-top: 100px;">
                        <img src="{{asset(config('view.frontend.wap_login').'/images/tu53.png')}}" alt="" width="250">
                        <p>您还没有收货地址，快去添加吧！</p>
                    </div>
                    @endif
                </div>


            </div>
        </div>
    </div>
    <div class="mui-bar mui-bar-footer mui-text-center " style="background: #5a92fd; height: 50px;line-height: 50px;">
        <a class="mui-text-bai" href="{{route('f_Wap_Address_create')}}" style="display: block;">添加新地址</a>
    </div>
@endsection
@push('scripts')
    <script>
        function optAddr(obj) {
            $.ajax({
                type: 'get',
                url: $(obj).data('url'),
            }).done(function (response) {
                if(response.status){
                    mui.toast(response.msg);
                    setTimeout(function () {
                        location.href = response.url;
                    }, 2000);
                }else{
                    mui.toast(response.msg);
                }
            }).fail(function (XMLHttpRequest) {
                if (XMLHttpRequest.status == 422) {
                    var errors = XMLHttpRequest.responseJSON.errors;
                    if(typeof errors == 'object') {
                        for (var index in errors) { // 不推荐这样
                            mui.toast(errors[index][0]);
                            break;
                        }
                    }else{
                        mui.toast(XMLHttpRequest.responseJSON.msg);

                    }
                }else{
                    mui.toast('网络异常,请检查连接');
                }

            }).always(function () {

            });
        }
    </script>
@endpush